package com.pxxy.springmvc.handlers;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/********************
 * 处理异常的第二种方式：
 * 可以实现全局的异常处理
 */
@Component
public class GlobalExceptionHandler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception e) {
        //
        final String requestURI = request.getRequestURI();
        final Object login_user = request.getSession().getAttribute("LOGIN_USER");
        // 当前时间

        System.out.println("造成异常的处理类："+handler.getClass());
        System.out.println("异常信息："+e.getClass()+" => "+ e.getMessage());

        //
        ModelAndView mv = new ModelAndView();
        mv.addObject("EX_MSG", e.getMessage());
        mv.setViewName("forward:/static/500.jsp");
        return mv;
    }
}
